package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/9/14 15:17
 * @Version 1.0
 */
@Controller
@RequestMapping("admin")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AdminController extends BaseController {

    @Reference
    private AdminService adminService;

    @Reference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PreAuthorize("hasAuthority('admin.show')")
    @RequestMapping
    public String find(HttpServletRequest request, Model model){

        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);

        model.addAttribute("page",page);
        model.addAttribute("filters",filters);

        return "admin/index";

    }

    @PreAuthorize("hasAuthority('admin.create')")
    @RequestMapping("create")
    public String create(){

        return "admin/create";

    }

    @PreAuthorize("hasAuthority('admin.create')")
    @RequestMapping("save")
    public String save(Admin admin,HttpServletRequest request){

        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        adminService.insert(admin);
        return "common/successPage";

    }

    @PreAuthorize("hasAuthority('admin.delete')")
    @RequestMapping("delete/{id}")
    public String save(@PathVariable Long id){

        adminService.delete(id);
        return "redirect:/admin";

    }

    @PreAuthorize("hasAuthority('admin.edit')")
    @RequestMapping("edit/{id}")
    public String edit(Model model,@PathVariable Long id){

        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);

        return "admin/edit";
    }

    @PreAuthorize("hasAuthority('admin.edit')")
    @RequestMapping("update")
    public String update(Admin admin){

        adminService.update(admin);
        return "common/successPage";

    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(Model model,@PathVariable Long id) {
        model.addAttribute("id", id);
        return "admin/upload";
    }

    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String newFileName =  UUID.randomUUID().toString() ;
        // 上传图片
        QiniuUtil.upload2Qiniu(file.getBytes(),newFileName);
        String url= "http://rigi1ax2n.hb-bkt.clouddn.com/"+ newFileName;
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl(url);
        adminService.update(admin);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('admin.assign')")
    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(Model model,@PathVariable Long adminId) {
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId", adminId);
        return "admin/assignShow";
    }

    @PreAuthorize("hasAuthority('admin.assign')")
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, Long[] roleIds) {
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }

}
